package iq.number;

import java.math.BigDecimal;

public class WhyUseBigDecimal {

	/**
	 * In certain situations, you need to show the exact decimal values. If you
	 * use the double data type, you need to truncate the decimal values. To
	 * avoid truncation while obtaining proper decimal values, you can use
	 * BigDecimal instead of double.
	 * 
	 * @author dingdahai
	 * @date Nov 6, 2009 9:31:25 AM
	 * @param args
	 */
	public static void main(String arg[]) {

		BigDecimal bd = new BigDecimal("0");
		bd = bd.add(new BigDecimal("99.99"));
		bd = bd.add(new BigDecimal("1.99"));
		System.out.println("BigDecimal result " + bd);// 101.98

		double d = 99.99;
		System.out.println("99.99+0.1="+(99.99+0.1) + ", 99.99+0.01=" + (99.99+0.01) );//
		d = d + 1.99;
		System.out.println("Double result " + d);// 101.97999999999999

		bd = new BigDecimal("0");
		bd = bd.add(new BigDecimal("99.99"));
		bd = bd.add(new BigDecimal(1.99));// 当以浮点型作参数时结果
		System.out.println("BigDecimal and double result " + bd);// 101.9799999999999999911182158029987476766109466552734375
	}

}
